from flask import Flask, json, request
from flask import render_template
from venv.include.forms import LoginForm
from sqlalchemy import (MetaData, Table, Column, Integer, Numeric, String, Boolean, DateTime, ForeignKey, create_engine)
from sqlalchemy import PrimaryKeyConstraint, UniqueConstraint, CheckConstraint, Index
from sqlalchemy import ForeignKey
from sqlalchemy import insert
from datetime import datetime
from sqlalchemy.sql import select

app = Flask(__name__)
# app.config.from_pyfile("config")
# @app.route('/')
# def hello_world():
#     return 'Hello, World!'


# @app.route("/")
# def index():
#     user = {"username": "bobo"}
#     posts = ["li", "xu", "wang", "zhang"]
#     return render_template("index2.html",
#                            title="测试1",
#                            posts=posts)


@app.route("/", methods=['GET', 'POST'])
def login_one():
    form = LoginForm()

    return render_template("login_form.html",
                           title="Sign in",
                           posts=["xu"],
                           form=form)


@app.route("/index", methods=["get"])
def index():
    res = {"msg": "这是一接口msg", "code": "0"}
    return res


@app.route("/login", methods=["get"])
def login():
    user = request.values.get("user")
    pd = request.values.get("pass")

    # 创建引擎
    engine = create_engine("sqlite:///test2.db")
    # 连接数据库
    connection = engine.connect()
    # 元数据
    metadata = MetaData()

    users = Table("users", metadata,
                  Column("user_id", Integer(), primary_key=True),
                  Column("user_name", String(20), nullable=False, unique=True),
                  Column("email_address", String(), nullable=False, unique=True),
                  Column("phone", String(20), nullable=False),
                  Column("password", String(25), nullable=False),
                  Column("created_on", DateTime(), default=datetime.now),
                  Column("updated_on", DateTime(), default=datetime.now, onupdate=datetime.now)
                  )
    list(users.columns)

    ins = insert(users).values(
        user_id=user,
        user_name=user,
        email_address=user,
        phone=user,
        password=pd,
    )
    result = connection.execute(ins)
    s = users.select()
    rp = connection.execute(s)
    result = rp.fetchall()
    return str(result)





